﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI.WebControls;

    #region Delegates

    public delegate void ImageEventHandler(object sender, ImageEventArgs e);

    #endregion Delegates

    public class ImageEventArgs : EventArgs
    {
        #region Fields

        private ImageAlign _align;
        private string _alternateText;
        private Unit _border;
        private Unit _height;
        private Unit _margin;
        private Unit _padding;
        private string _src;
        private Unit _width;

        #endregion Fields

        #region Constructors

        public ImageEventArgs(string src, string alternateText, Unit width, 
            Unit height, ImageAlign align, Unit border,
            Unit margin, Unit padding)
        {
            _src = src;
            _alternateText = alternateText;
            _width = width;
            _height = height;
            _align = align;
            _border = border;
            _margin = margin;
            _padding = padding;
        }

        internal ImageEventArgs(string postbackData)
        {
            DeserializePostbackData(postbackData);
        }

        #endregion Constructors

        #region Properties

        public ImageAlign Align
        {
            get { return _align; }
            set { _align = value; }
        }

        public string AlternateText
        {
            get { return _alternateText; }
            set { _alternateText = value; }
        }

        public Unit Border
        {
            get { return _border; }
            set { _border = value; }
        }

        public Unit Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public Unit Margin
        {
            get { return _margin; }
            set { _margin = value; }
        }

        public Unit Padding
        {
            get { return _padding; }
            set { _padding = value; }
        }

        public string Src
        {
            get { return _src; }
            set { _src = value; }
        }

        public Unit Width
        {
            get { return _width; }
            set { _width = value; }
        }

        #endregion Properties

        #region Methods

        internal void DeserializePostbackData(string postbackData)
        {
            string[] keyValuePair = postbackData.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in keyValuePair)
            {
                string[] pair = s.Split(':');
                string value = pair[1];
                switch (pair[0])
                {
                    case "src":
                        _src = value;
                        break;
                    case "alt":
                        _alternateText = value;
                        break;
                    case "width":
                        _width = Unit.Parse(value);
                        break;
                    case "height":
                        _height = Unit.Parse(value);
                        break;
                    case "align":
                        if (!value.Equals("none",StringComparison.OrdinalIgnoreCase)&&!string.IsNullOrEmpty(value))
                            _align = (ImageAlign)Enum.Parse(typeof(ImageAlign), value);
                        else
                            _align = ImageAlign.NotSet;
                        break;
                    case "border":
                        _border = Unit.Parse(value);
                        break;
                    case "margin":
                        _margin = Unit.Parse(value);
                        break;
                    case "padding":
                        _padding = Unit.Parse(value);
                        break;
                }
            }
        }

        #endregion Methods
    }
}